#ifndef DVMAINWINDOW_H
#define DVMAINWINDOW_H

#include <QMainWindow>
#include "dvDatabaseController.h"
#include "dvDataTableController.h"

namespace Ui {
class dvMainWindow;
}

class dvMapView;
class dvMapScene;
class QColorRampEditor;
class dvLineChart;
class dvHistogram;
class dvPieChart;

class dvMainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit dvMainWindow(QWidget *parent = 0);
    ~dvMainWindow();
    
private:
    Ui::dvMainWindow *ui;
    QComboBox        *m_pMapsList;
    QTableWidget     *m_pDataTable;
    dvMapView        *m_pMapView;
    dvMapScene       *m_pMapScene;

    dvDatabaseController  m_dbCtrl;
    dvDataTableController m_dtCtrl;

    bool m_isCheckDataEnable;

    inline void setCheckDataEnable(bool check){ m_isCheckDataEnable = check; }
    inline bool isCheckDataEnable(){ return m_isCheckDataEnable; }

    void setEncoding();
    void connectObjects();
    void connectActions();

    int currentTabIndex();

    // TAB_DATA_TABLE

    void loadMaps();
    void reloadMaps();
    void lockMapsList(bool lock);
    int  currentMapId();
    QString currentMapName();
    bool setCurrentMap(int id);

    void initDataTable();
    void changeTableDelegate(int mapId);
    void setCorrectRegionsInTable();
    void loadData(const DV_INDICATOR& indicator, QSqlQuery *query, int records);
    void importData(const QList<QVariantList>* list, const QString& dateFormat);
    void insertRowData(int indicatorColumn, const DV_DATA& data);
    void setRowColor(int row, const QColor& color);
    void fillEmptyCells();
    int  isCorrectRowData(int row);
    bool isRepeatDataInTableForRow(int row);
    bool commitData();
    void checkData();
    void clearTable();
    void exportData(int format, QString file);

    void deleteRowRecors(int row);
    void updateRowRecord(int row, int col);
    void deleteColumnRecords(int col);

    void openTable(const QString& fname);
    void saveTable(const QString& fname);

    // TAB_MAP

    QStandardItemModel m_RegionsListModel;
    QColorRampEditor*  m_pColorRamp;
    QLabel*            m_pMapInfoLabel;
    bool m_mapFileExist;
    bool m_repeat;
    bool m_play;
    int  m_speed;
    QTimer *m_pPlayTimer;

    void initMapWidget();
    void initRegionsList(QVariant indicatorId, QString date);
    void clearRegionsList();
    void initIndicatorsList();
    void initDateSlider();
    void loadVectorMap(QVariant mapId);
    void setMapInfoText(const QString& text);
    void exportMap();

    Regions currentRegions();

    // TAB_LINE_CHART

    dvLineChart *m_pLineChart;

    void initLineChart();
    void loadLineChartCurves(QVariant indicatorId, QString mapName, QString indicatorName);
    void exportQwtPlot(QwtPlot* plot);

    // TAB_HISTOGRAM

    dvHistogram *m_pHistogram;

    void initHistogram();
    void loadHistogramBars(QVariant indicatorId, QString mapName, QString indicatorName, QString date);

    // TAB_PIE_CHART

    dvPieChart *m_pPieChart;

    void initPieChart();
    void loadPieces(QVariant indicatorId, QString mapName, QString indicatorName, QString date);
    void exportPieChart();

private slots:
    void clickedNewTable();
    void clickedOpen();
    void clickedSave();
    void clickedSaveAs();
    void clickedImport();
    void clickedExport();
    void clickedFromDB();
    void clickedAddRow();
    void clickedDeleteRow();
    void clickedDeleteRowFromDB();
    void clickedNewIndicator();
    void clickedEditIndicator();
    void clickedDeleteIndicator();
    void clickedDeleteIndicatorFromDB();
    void clickedUpdateTable();
    void clickedCheckTable();
    void clickedMapsEdit();
    void clickedRegionsEdit();
    void clickedSynonyms();
    void clickedExit();
    void clickedSectionHeader(int index);
    void clickedDateBack();
    void clickedDateForward();
    void clickedPlay(bool state);
    void clickedRepeat(bool state);
    void currentMapChanged(int index);
    void currentCellChanged(int row, int column);
    void currentTabWidgetChanged(int index);
    void currentIndicatorChanged(int index);
    void clickedRegionsList(QModelIndex index);
    void checkAllRegions(bool state);
    void valueSpeedChanged(int value);
    void valueDateChanged(int value);
    void updateColorRamp();
    void updateAllTable();
    void polygonClicked(QVariant id, bool check);
    void play();
    void clickedLegendLineChart(bool check);
    void clickedSpline(bool check);
    void clickedLegendHistogram(bool check);
    void clickedLegendPieChart(bool check);
    void clickedRoundedLegendPieChart(bool check);
    void clicked3dPieChart(bool check);
    void clickedAbout();
    void clickedAboutQt();
};

#endif // DVMAINWINDOW_H
